package com.visionet.waveletData.web.entity.account;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.visionet.waveletData.web.entity.IdEntity;

/**
 * 用户.
 * 
 * @author xt
 */
@Entity
@Table(name = "p_user")
// 默认的缓存策略.
//@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends IdEntity {
	
	public User(){
	}

	public User(Long id) {
		this.id = id;
	}
	
	private String user_name;
	private String pass_word;
	private String company_id;
	private String user_type;
	private Date last_login_time;
	
	
	/*private String loginName;
	private String aliasName;
	private String password;
	private String plainPassword;
	private String passwordSalt;
	
	private Long orgId=0L;
	private String orgName;

	private Long secId;
	private Integer isLockSec;
	private Integer isLock;
	private Date lastLogin;
	
	private Set<Role> roleList = new HashSet<Role>();
	private String roleIds;
	private UserInfo userInfo;
	
	@Transient
	public String getPlainPassword() {
		return plainPassword;
	}

	public void setPlainPassword(String plainPassword) {
		this.plainPassword = plainPassword;
	}

	
	@NotBlank
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getAliasName() {
		return aliasName;
	}

	public void setAliasName(String aliasName) {
		this.aliasName = aliasName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Long getSecId() {
		return secId;
	}

	public void setSecId(Long secId) {
		this.secId = secId;
	}

	@Column(insertable=false)
	public Integer getIsLockSec() {
		return isLockSec;
	}

	public void setIsLockSec(Integer isLockSec) {
		this.isLockSec = isLockSec;
	}

	public Integer getIsLock() {
		return isLock;
	}

	public void setIsLock(Integer isLock) {
		this.isLock = isLock;
	}

	@Column(insertable=false)
	@JsonSerialize(using = JsonDateSerializer.class)
	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}
	
	

	public String getPasswordSalt() {
		return passwordSalt;
	}

	public void setPasswordSalt(String passwordSalt) {
		this.passwordSalt = passwordSalt;
	}

	// 多对多定义
	@GeneratedValue(strategy = GenerationType.AUTO)
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "c_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序
	@OrderBy("id ASC")
	// 缓存策略
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@JsonIgnore
	public Set<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(Set<Role> roleList) {
		this.roleList = roleList;
	}
	



	@Transient
	public String getRoleNames() {
		if(roleList == null) return null;
		return Collections3.extractToString(roleList, "name", ", ");
	}
	
	@Transient
	@JsonIgnore
	public String getRoleDescs() {
		if(roleList == null) return null;
		return Collections3.extractToString(roleList, "roleDesc", ", ");
	}
	
	@Transient
	public String getRoleIds() {
		return roleIds;
	}
	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}


	
	@OneToOne(cascade={CascadeType.ALL}, fetch = FetchType.LAZY, optional = false)
	@PrimaryKeyJoinColumn
	@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})
	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	
	
	@Column(updatable=false)
	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}
	
	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}*/
	@Override
    public int hashCode(){
        return this.getId()==null?1:this.getId().hashCode();
    }
	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getPass_word() {
		return pass_word;
	}

	public void setPass_word(String pass_word) {
		this.pass_word = pass_word;
	}

	public String getCompany_id() {
		return company_id;
	}

	public void setCompany_id(String company_id) {
		this.company_id = company_id;
	}

	public String getUser_type() {
		return user_type;
	}

	public void setUser_type(String user_type) {
		this.user_type = user_type;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	@Override
	public boolean equals(Object obj) {
		if( null== obj||!( obj instanceof User)){
			return false;
		}
		User user = (User)obj;
		if(user.getId()==null){
			return false;
		}
		return user.getId().equals(this.id);
	}

	public Date getLast_login_time() {
		return last_login_time;
	}

	public void setLast_login_time(Date last_login_time) {
		this.last_login_time = last_login_time;
	}	
}